<?php
include "mysql_config.php";

set_time_limit(0);
date_default_timezone_set("Asia/Shanghai");
header("Content-Type: text/html; charset=UTF-8");
$mysqlConfig = new MysqlConfig();
$mysqli = new mysqli($mysqlConfig->getMysqlServerName(), $mysqlConfig->getMysqlUserName(), $mysqlConfig->getMysqlPassword(), $mysqlConfig->getMysqlDatabase());
$yesterday = date('Y-m-d',strtotime("-1day"));
$startTime = $yesterday . " 00:00:00";
$lastTime = $yesterday . " 23:59:59";
$supermarketConfigSql = "select * from 91fbw_supermarket_config where status != 'wait_open' and status != 'wait_show' and status != 'close'; ";
$supermarketConfigResult = $mysqli->query($supermarketConfigSql);
while ($row = mysqli_fetch_assoc($supermarketConfigResult)) {
    $cityId = $row['cityid'];
    //准备好数据
    $configContent = [
        'validdays' => $row['validdays'],
        'freecouponnumber' => $row['freecouponnumber'],
        'rechargecouponnumber' => $row['rechargecouponnumber'],
        'remindlines' => $row['remindlines'],
        'remindphone' => $row['remindphone']
    ];
    $configDescription = json_encode($configContent);
    //检测昨天是不是有数据
    $isExitSupermarketDailySql = "select count(*) as count from 91fbw_supermarket_daily where cityid=". $cityId . " and date = '" . $yesterday . "';";
    $isExitSupermarketDailyResult = $mysqli->query($isExitSupermarketDailySql);
    $isExistSupermarketDailyStatus = 0;
    while ($supermarketDailyRow = mysqli_fetch_assoc($isExitSupermarketDailyResult)) {
        $isExistSupermarketDailyStatus = ($supermarketDailyRow['count'] > 0) ? 1 : 0;
    }
    if($isExistSupermarketDailyStatus) {
        continue;
    }
    //发出的体验卡（无门槛）
    $freeSendNumber = 0;
    $freeSendNumberSql = "select count(*) as count from 91fbw_supermarket_coupon where created_at >='". $startTime .
        "' and created_at<='" . $lastTime . "' and cityid = " . $cityId . " and type = 'free';";
    $freeSendNumberResult = $mysqli->query($freeSendNumberSql);
    while ($freeSendRow = mysqli_fetch_assoc($freeSendNumberResult)) {
        $freeSendNumber = $freeSendRow['count'];
        $freeSendNumber = $freeSendNumber ? $freeSendNumber: 0;
    }
    //发出的体验卡（充值兑换）
    $rechargeSendNumber = 0;
    $rechargeSendNumberSql = "select count(*) as count from 91fbw_supermarket_coupon where created_at >= '". $startTime .
        "' and created_at<='" . $lastTime . "' and cityid = " . $cityId . " and type = 'recharge';";
    $rechargeSendNumberResult = $mysqli->query($rechargeSendNumberSql);
    while ($rechargeSendRow = mysqli_fetch_assoc($rechargeSendNumberResult)) {
        $rechargeSendNumber = $rechargeSendRow['count'];
        $rechargeSendNumber = $rechargeSendNumber ? $rechargeSendNumber :0;
    }
    //已使用的体验卡（无门槛）
    $freeUseNumber = 0;
    $freeUseNumberSql = "select count(*) as count from 91fbw_supermarket_coupon where usetime >= '". $startTime .
        "' and usetime<='" . $lastTime . "' and cityid = " . $cityId . " and type = 'free' and status=1;";
    $freeUseNumberResult = $mysqli->query($freeUseNumberSql);
    while ($freeUseRow = mysqli_fetch_assoc($freeUseNumberResult)) {
        $freeUseNumber = $freeUseRow['count'];
        $freeUseNumber = $freeUseNumber ? $freeUseNumber :0;
    }
    //已使用的体验卡（充值兑换）
    $rechargeUseNumber = 0;
    $rechargeUseNumberSql = "select count(*) as count from 91fbw_supermarket_coupon where usetime >= '". $startTime .
        "' and usetime <='" . $lastTime . "' and cityid = " . $cityId . " and type = 'recharge' and status=1;";
    $rechargeUseNumberResult = $mysqli->query($rechargeUseNumberSql);
    while ($rechargeUseRow = mysqli_fetch_assoc($rechargeUseNumberResult)) {
        $rechargeUseNumber = $rechargeUseRow['count'];
        $rechargeUseNumber = $rechargeUseNumber ? $rechargeUseNumber:0;
    }
    //代理商补贴（元）
    $subsidy = 0;
    $subsidySql = "select sum(subsidy) as sum from 91fbw_supermarket_coupon where usetime >= '". $startTime .
        "' and usetime <='" . $lastTime . "' and cityid = " . $cityId . " and type = 'recharge' and status=1;";
    $subsidyResult = $mysqli->query($subsidySql);
    while ($subsidyRow = mysqli_fetch_assoc($subsidyResult)) {
        $subsidy = $subsidyRow['sum'];
        $subsidy = $subsidy ? $subsidy :0.00;
    }
    $supermarketDailySql = "insert into 91fbw_supermarket_daily (`cityid`, `date`, `freesendnumber`, `rechargesendnumber`, `freeusenumber`, `rechargeusenumber`, `subsidy`, `content`, `created_at`, `updated_at`)" .
        " values (". $cityId .", '". $yesterday ."', ". $freeSendNumber.", ".$rechargeSendNumber.", ".$freeUseNumber.", ".$rechargeUseNumber.", ".$subsidy.", '".$configDescription."', '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s')."');";
    $insertSupermarketDailyResult = $mysqli->query($supermarketDailySql);

}
$mysqli->close();